package com.sxbbc.seller.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.dto.jz.QueryOrderMoneyVo;
import com.sxbbc.common.core.entity.jz.OrderMain;
import com.sxbbc.common.core.enums.OrderStatusEnums;
import com.sxbbc.common.core.enums.OrderTypeEnums;
import com.sxbbc.common.core.mapper.jz.OrderMainMapper;

/**
 * 商家统计 service
 * @author xiaoky
 * @date 2019-07-17
 */
@Service
public class StatisService {
	@Autowired
	private OrderMainMapper orderMainMapper;
	
	/**
	 * 查询商家主页订单数据
	 * @return Map
	 * @author xiaoky
	 */
	public Map<String, Object> queryOrderMsg(Integer bid) {
		if (bid == null) {
			bid = 0;
		}
		Map<String, Object> map = new HashMap<>();
		QueryOrderMoneyVo vo = new QueryOrderMoneyVo();
		vo.setBid(bid);
		//当天
		this.queryOrderSatisByDay(vo,map);

		//当月
		this.queryOrderSatisByMonth(bid,vo,map);

		//退款
		this.queryOrderRefundSatis(bid,vo,map);
		return map;
	}

	/**
	 * 退款情况
	 * @param vo
	 * @param map
	 */
	private void queryOrderRefundSatis(Integer bid,QueryOrderMoneyVo vo, Map<String, Object> map) {
		vo = new QueryOrderMoneyVo();
		vo.setBid(bid);
		vo.setOrderStatus(OrderStatusEnums.REFUND.getId());
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));

		//当月退款总金额
		vo.setType(StaticUtils.MONTH_DATA);
		BigDecimal monthRefundOrderMoney = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("monthRefundOrderMoney", monthRefundOrderMoney);

		//当月退款总数量
		vo.setType(StaticUtils.MONTH_DATA);
		int monthRefundOrderNum = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("monthRefundOrderNum", monthRefundOrderNum);

		//当天退款数量
		vo.setType(StaticUtils.DAY_DATA);
		int todayRefundOrderNum = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("todayRefundOrderNum", todayRefundOrderNum);
	}

	/**
	 * 当月订单情况
	 * @param vo
	 * @param map
	 */
	private void queryOrderSatisByMonth(Integer bid,QueryOrderMoneyVo vo, Map<String, Object> map) {
		vo = new QueryOrderMoneyVo();
		vo.setBid(bid);
		vo.setType(StaticUtils.MONTH_DATA);
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));

		//当月订单总数量
		int odernumByMonth = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("odernumByMonth", odernumByMonth);

		//当月订单总金额
		vo.setOrderStatus(OrderStatusEnums.OVER.getId());
		BigDecimal monthOrderMoney = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("monthOrderMoney", monthOrderMoney);

		//当月订单完成数量
		vo.setOrderStatus(OrderStatusEnums.OVER.getId());
		int finishOdernumByMonth = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("finishOdernumByMonth", finishOdernumByMonth);
	}

	/**
	 * 查询当天的订单金额
	 * @param vo
	 * @param map
	 */
	private void queryOrderSatisByDay(QueryOrderMoneyVo vo, Map<String, Object> map) {
		vo.setTime(DateUtil.getInstance().toDateString(new Date()));
		vo.setType(StaticUtils.DAY_DATA);

		//当天订单总数量
		int totalNum = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("totalNum", totalNum);

		//当天完成订单总金额
		vo.setOrderStatus(OrderStatusEnums.OVER.getId());
		BigDecimal todayOrderMoney = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
		map.put("todayOrderMoney", todayOrderMoney);

		//当天订单完成数量
		int finishNum = orderMainMapper.countOrderNumByTypeAndTime(vo);
		map.put("finishNum", finishNum);
	}

	/**
	 * 查询首页订单数据
	 * @param bid
	 * @author xiaoky
	 */
	public List<Map<String, Object>> queryHomeOrderMsg(Integer bid) {
		if (bid == null) {
			bid = 0;
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (OrderTypeEnums orderType: OrderTypeEnums.queryValidList()) {
			Map<String, Object> map = new HashMap<String, Object>();
			QueryWrapper<OrderMain> queryWrapper = new QueryWrapper<OrderMain>();
			queryWrapper.eq("bid", bid).ne("order_status", OrderStatusEnums.NEED_SEND.getId()).eq("order_type", orderType.getId());
			QueryOrderMoneyVo vo = new QueryOrderMoneyVo();
			vo.setBid(bid);
			vo.setOrderType(orderType.getId());
			BigDecimal money = orderMainMapper.queryOrderMoneyByTypeAndTime(vo);
			map.put("value", money);
			map.put("name", orderType.getName());
			list.add(map);
		}
		return list;
	} 
}
